package com.message.action;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.message.entity.Message;
import com.message.service.MessageService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class MessageAction extends ActionSupport {
	private Message message;
	private MessageService mService;
	private List<Message> mlist;
	private int pageNow = 1;
	private int pageSize = 4;

	public MessageAction() {
		BeanFactory bf = new ClassPathXmlApplicationContext("applicationContext.xml");
		mService = (MessageService) bf.getBean("messageService");
	}

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public List<Message> getMlist() {
		return mlist;
	}

	public void setMlist(List<Message> mlist) {
		this.mlist = mlist;
	}

	public int getPageNow() {
		return pageNow;
	}

	public void setPageNow(int pageNow) {
		this.pageNow = pageNow;
	}

	public String add() throws Exception {
		message.setCreatetime(new Timestamp(System.currentTimeMillis()));
		if (mService.addMessage(message) > 0) {
			return "get";
		}
		return Action.ERROR;
	}

	public String get() throws Exception {
		mlist = mService.getMessages(pageNow, pageSize);
		return Action.SUCCESS;
	}

}
